1592D - Hemose in ICPC - CodeForces Solution


binary search dfs and similar implementation interactive math number theory trees *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define N 502000

using namespace std;
struct edge{
    int to,next;
}e[N<<1];
const int mo=1000000007;
inline int read(){
    int x=0,w=0;char ch=getchar();
    while (!isdigit(ch))w|=ch=='-',ch=getchar();
    while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return w?-x:x;
}
int n,head[N],cnt,c[N],tot;
pii ed[N];
void add(int u,int v){
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void dfs(int u,int fa){
    for (int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if (v==fa)continue;
        ed[++tot]=(pii){u,v};
        dfs(v,u);
    }
}
int ck(int l,int r){
    int sum=0;
    for (int i=1;i<=n;++i)c[i]=0;
    for (int i=l;i<=r;++i)c[ed[i].fi]=c[ed[i].se]=1;
    for (int i=1;i<=n;++i)sum+=c[i];
    cout<<"? "<<sum;
    for (int i=1;i<=n;++i)if (c[i])cout<<" "<<i;
    cout<<endl;
    int x;cin>>x;
    return x;
}
signed main(){
    n=read();int mx=0;
    for (int i=1;i<n;++i){
        int u=read(),v=read();
        add(u,v);add(v,u);
    }
    dfs(1,0);
    cout<<"? "<<n;
    for (int i=1;i<=n;++i)cout<<" "<<i;cout<<endl;
    cin>>mx;
    int l=1,r=n-1,res;
    while (l<=r){
        int mid=(l+r)>>1;
        if (ck(l,mid)==mx)r=mid-1,res=mid;
        else l=mid+1,res=mid+1;
    }
    cout<<"! "<<ed[res].fi<<" "<<ed[res].se<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

740A - Alyona and copybooks
1491A - K-th Largest Value
922B - Magic Forest
922D - Robot Vacuum Cleaner
408B - Garland
1391A - Suborrays
1700C - Helping the Nature
982A - Row
877A - Alex and broken contest
919D - Substring
362B - Petya and Staircases
1535C - Unstable String
1738F - Connectivity Addicts
1342B - Binary Period
1551C - Interesting Story
794B - Cutting Carrot
534B - Covered Path
1278C - Berry Jam
1203A - Circle of Students
1740B - Jumbo Extra Cheese 2
1740A - Factorise N+M
49B - Sum
23A - You're Given a String
1105C - Ayoub and Lost Array
1624E - Masha-forgetful
998B - Cutting
250A - Paper Work
1740C - Bricks and Bags
1130A - Be Positive
465A - inc ARG